package com.bishe.demo.modules.tourism.dao;

import com.bishe.demo.modules.account.entity.User;
import com.bishe.demo.modules.tourism.entity.Route;
import com.bishe.demo.modules.tourism.entity.RouteSearchBean;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Description RouteDao
 * @Author Lubb
 * @Date 2021/3/29 14:11
 */
@Repository
@Mapper
public interface RouteDao {
    @Insert("insert into tourism_route(title,introduction,start_point,terminal_point,type,price,level,fk_user_id,start_date,terminal_date," +
            "max_people) VALUES(#{title},#{introduction},#{startPoint},#{terminalPoint},#{type},#{price},#{level}," +
            "#{fk_user_id},#{startDate},#{terminalDate},#{max_people})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void addRoute(Route route);

    @Delete("delete from tourism_route where id = #{id}")
    void deleteRouteById(Integer id);

    @Delete("delete from tourism_route where fk_supplier_id = #{supplierId}")
    void deleteRouteByUserId(Integer supplierId);

    @Update("update tourism_route set title=#{title},introduction=#{introduction},start_point=#{startPoint},start_date=#{startDate},terminal_date=#{terminalDate}" +
            "terminal_point=#{terminalPoint},type=#{type},price=#{price},level=#{level},max_people=#{max_people}," +
            "fk_user_id=#{fk_user_id} where id = #{id}")
    void updateRoute(Route route);

    @Select("select * from tourism_route where id = #{id}")
    @Results(id = "baseResult",value = {
            @Result(column = "id",property = "id"),
            @Result(column = "max_people",property = "max_people"),
            @Result(column = "id",property = "viaPoint",javaType = List.class,
            many = @Many(select = "com.bishe.demo.modules.common.dao.CityDao.getCitiesByRouteId")),
            @Result(column = "id",property = "tourismImage",javaType = List.class,
            many = @Many(select = "com.bishe.demo.modules.tourism.dao.TourismImageDao.getTourismImagesByRouteId")),
            @Result(column = "id",property = "user",javaType = User.class,
            many = @Many(select = "com.bishe.demo.modules.account.dao.UserDao.getUserByRouteId")),
            @Result(column = "id",property = "processes",javaType = List.class,
            many = @Many(select = "com.bishe.demo.modules.tourism.dao.ProcessDao.getProcessesByRouteId")),
            @Result(column = "id", property = "routeEvaluations", javaType = List.class,
            many = @Many(select = "com.bishe.demo.modules.tourism.dao.RouteEvaluationDao.getRouteEvaluationsByRouteId"))
    })
    Route getRouteById(Integer Id);

    @Select("select * from tourism_route")
    @ResultMap("baseResult")
    List<Route> getRoutes();

    @Select("<script>" +
            "select * from tourism_route "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (title like '%${keyword}%') "
            + "</if>"
            + "<if test='startPoint != \"\" and startPoint != null'>"
            + " and (start_point like '%${startPoint}%') "
            + "</if>"
            + "<if test='terminalPoint != \"\" and terminalPoint != null'>"
            + " and (terminal_point like '%${terminalPoint}%') "
            + "</if>"
            + "<if test='startDate != \"\" and startDate != null'>"
            + " and (start_date = #{startDate} ) "
            + "</if>"
            + "<if test='terminalDate != \"\" and terminalDate != null'>"
            + " and (terminal_date = #{terminalDate}) "
            + "</if>"
            + "<if test='type != \"\" and type != null'>"
            + " and (type like '%${type}%') "
            + "</if>"
            + "<if test='maxPrice != \"\" and maxPrice != null and minPrice != \"\" and minPrice != null'>"
            + " and (price between #{minPrice} and #{maxPrice}) "
            + "</if>"
            + "<if test='level != \"\" and level != null'>"
            + " and (level = #{level}) "
            + "</if>"
            + "<if test='maxPeople != \"\" and maxPeople != null'>"
            + " and (max_people = #{maxPeople}) "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id asc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    @ResultMap("baseResult")
    List<Route> getRoutesBySearchBean(RouteSearchBean searchBean);
}
